package com.telchina.workorder.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telchina.common.core.controller.BaseController;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.domain.WoAttachment;
import com.telchina.common.enums.WorkOrderStatusEnum;
import com.telchina.common.service.IBaseFlowService;
import com.telchina.common.service.IWoAttachmentService;
import com.telchina.common.utils.SecurityUtils;
import com.telchina.getui.service.CidUidRelationService;
import com.telchina.rcbg.domain.KnowledgeInfo;
import com.telchina.rcbg.domain.ProclamationInfo;
import com.telchina.rcbg.service.IKnowledgeInfoService;
import com.telchina.rcbg.service.IProclamationInfoService;
import com.telchina.workorder.domain.*;
import com.telchina.workorder.domain.vo.*;
import com.telchina.workorder.service.*;
import com.telchina.workorder.third.service.BpmTaskThirdService;
import com.telchina.workorder.third.service.SystemThirdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "移动端接口v1")
@RestController
@RequestMapping("/v1/app")
public class AppController extends BaseController {

    @Autowired
    private IWoWorkOrderService woWorkOrderService;

    @Autowired
    private SystemThirdService systemThirdService;

    @Autowired
    private IWoSuperviseService woSuperviseService;

    @Autowired
    private IWoExtensionService woExtensionService;

    @Autowired
    private IWoDisregardKpiService disregardKpiService;

    @Autowired
    private BpmTaskThirdService taskThirdService;

    @Autowired
    private IWoReplyService woReplyService;

    @Autowired
    private IWoReturnVisitService woReturnVisitService;

    @Autowired
    private IWoAttachmentService woAttachmentService;

    @Resource
    private IKnowledgeInfoService knowledgeInfoService;

    @Resource
    private IProclamationInfoService proclamationInfoService;

    @Resource
    private IBaseFlowService baseFlowService;

    @Resource
    private CidUidRelationService cidUidRelationService;

    @ApiOperation("获取 Todo 待办任务分页-工单")
    @GetMapping("workOrderList")
    public AjaxResult list(WoWorkOrderReqVO woWorkOrder) {
        List<WoWorkOrderVO> list = woWorkOrderService.selectListAll(woWorkOrder);
        return getDataTable(list);
    }

    @ApiOperation("获取 Todo 待办任务工单总数、临期、超期")
    @GetMapping("todoWorkOrderListNum")
    public AjaxResult todoWorkOrderListNum(WoWorkOrderReqVO woWorkOrder) {
        return woWorkOrderService.todoWorkOrderListNum(woWorkOrder);
    }

    @ApiOperation("获取 Todo 待办任务分页-工单")
    @GetMapping("todoWorkOrderList")
    public AjaxResult todoWorkOrderList(WoWorkOrderReqVO woWorkOrder) {
        List<WoWorkOrderVO> list = woWorkOrderService.selectTodoList(woWorkOrder);
        return getDataTable(list);
    }

    @ApiOperation("获取 Todo 待办任务分页-督办")
    @GetMapping("todoSuperviseList")
    public AjaxResult todoSuperviseList(WoSupervise supervise) {
        List<WoSuperviseVO> list = woSuperviseService.selectTodoList(supervise);
        return getDataTable(list);
    }

    @ApiOperation("获取 Todo 待办任务分页-延期申请")
    @GetMapping("todoExtensionList")
    public AjaxResult todoExtensionList(WoExtension extension) {
        List<WoExtensionVO> list = woExtensionService.selectTodoList(extension);
        return getDataTable(list);
    }

    @ApiOperation("获取 Todo 待办任务分页-不计考核")
    @GetMapping("todoDisregardKpiList")
    public AjaxResult todoDisregardKpiList(WoDisregardKpiReqVO supervise) {
        List<WoDisregardKpiVO> list = disregardKpiService.selectTodoList(supervise);
        return getDataTable(list);
    }

    @ApiOperation("获取工单详情")
    @GetMapping(value = "/workOrderNumber/{workOrderNumber}")
    @ApiImplicitParam(name = "workOrderNumber", value = "工单编号", required = true, dataTypeClass = String.class)
    public AjaxResult getWorkOrderByWorkOrderNumber(@PathVariable("workOrderNumber") String workOrderNumber) {
        return AjaxResult.success(woWorkOrderService.getWorkOrderByWorkOrderNumber(workOrderNumber));
    }

    @ApiOperation("获取流程审批详情")
    @GetMapping("listByProcessInstanceId")
    @ApiImplicitParam(name = "processInstanceId", value = "流程实例id", required = true, dataTypeClass = String.class)
    public AjaxResult getTaskListByProcessInstanceId(String processInstanceId) {
        return AjaxResult.success(woWorkOrderService.getTaskListByProcessInstanceId(processInstanceId));
    }

    @ApiOperation("获取下一步审批信息")
    @GetMapping("getNextUserTask")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processInstanceId", value = "流程实例id", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "operate", value = "操作", allowableValues = "", required = true, dataTypeClass = String.class)
    })
    public AjaxResult getNextUserTask(String processInstanceId, String operate) {
        Map<String, Object> params = new HashMap<>();
        params.put("processInstId", processInstanceId);
        params.put("operate", operate);
        return AjaxResult.success(taskThirdService.getNextUserTask(params));
    }

    @ApiOperation("延期申请保存并启动流程")
    @PostMapping("extension/add")
    public AjaxResult extensionAdd(@RequestBody WoExtension woExtension) throws Exception {
        return woExtensionService.insert(woExtension);
    }

    @ApiOperation("新增回复单")
    @PostMapping("reply/add")
    public AjaxResult replyAdd(@RequestBody WoReply woReply) throws Exception {
        return woReplyService.insert(woReply);
    }

    @ApiOperation("新增加回访单")
    @PostMapping("returnVisit/add")
    public AjaxResult add(@RequestBody WoReturnVisit woReturnVisit) throws Exception {
        return woReturnVisitService.insert(woReturnVisit);
    }

    @ApiOperation("延期申请单列表")
    @GetMapping("/extensionList/all")
    public AjaxResult listAll(WoExtension woExtension) {
        List<WoExtensionVO> listVO = woExtensionService.selectListAllAndAttachment(woExtension);
        return AjaxResult.success(listVO);
    }

    @ApiOperation("回复单列表")
    @GetMapping("/replyList/all")
    public AjaxResult listAll(WoReply woReply) {
        List<WoReplyVO> listVO = woReplyService.selectListAllAndAttachment(woReply);
        return AjaxResult.success(listVO);
    }

    @ApiOperation("回访单列表")
    @GetMapping("/returnVisitList/all")
    public AjaxResult listAll(WoReturnVisit woReturnVisit) {
        List<WoReturnVisit> list = woReturnVisitService.selectListAll(woReturnVisit);
        return AjaxResult.success(list);
    }

    @ApiOperation("修改工单信息")
    @PutMapping("woWorkOrder")
    public AjaxResult edit(@RequestBody WoWorkOrder woWorkOrder) {
        return woWorkOrderService.update(woWorkOrder);
    }

    @ApiOperation("新增工单信息")
    @PostMapping("woWorkOrder")
    public AjaxResult add(@RequestBody WoWorkOrder woWorkOrder) {
        if (woWorkOrder.getWorkOrderStatus().equals(WorkOrderStatusEnum.FINISH.getCode())) {
            woWorkOrder.setCompletionTime(new Date());
        }
        woWorkOrder.setRegistrant(SecurityUtils.getLoginUser().getUserId());
        woWorkOrder.setRegistrantDeptId(systemThirdService.getLoginUseDeptId());
        return woWorkOrderService.insert(woWorkOrder);
    }

    /**
     * 获取附件信息列表
     *
     * @param woAttachment
     * @return
     * @author konghao
     */
    @ApiOperation("获取附件信息列表")
    @GetMapping("attachment/list/all")
    public AjaxResult listAll(WoAttachment woAttachment) {
        List<WoAttachment> list = woAttachmentService.selectListAll(woAttachment);
        return AjaxResult.success(list);
    }

    /**
     * 知识分页查询
     *
     * @param pageNum    页码
     * @param pageSize   每页条数
     * @param queryParam 查询条件
     * @return
     */
    @ApiOperation("知识分页查询")
    @GetMapping("knowledgeInfo/pageList")
    public AjaxResult pageList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                               KnowledgeInfo queryParam) {
        IPage<KnowledgeInfo> page = knowledgeInfoService.pageList(pageNum, pageSize, queryParam);
        return AjaxResult.success(page);
    }

    @ApiOperation("获取流程处理过程中的按钮")
    @ApiImplicitParam(name = "taskDefinitionkey", value = "任务节点定义id，可在流程审批记录中第一条没有处理的任务记录中获取", required = true, dataTypeClass = String.class)
    @GetMapping("/getOptButton/{taskDefinitionkey}")
    public AjaxResult getOptButton(@PathVariable String taskDefinitionkey) {
        return baseFlowService.getOptButton(taskDefinitionkey);
    }

    /**
     * 公告分页查询
     *
     * @param pageNum    页码
     * @param pageSize   每页条数
     * @param queryParam 查询条件
     * @return
     */
    @ApiOperation("公告分页查询")
    @GetMapping("proclamationInfo/pageList")
    public AjaxResult pageList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                               ProclamationInfo queryParam) {
        IPage<ProclamationInfo> page = proclamationInfoService.pageList(pageNum, pageSize, queryParam);
        return AjaxResult.success(page);
    }

    @ApiOperation("公告详情")
    @GetMapping("proclamationInfo/getByIdWithAttachment/{id}")
    public AjaxResult getProclamationInfo(@PathVariable("id") String id) {
        ProclamationInfo info = proclamationInfoService.getByIdWithAttachment(id);
        return AjaxResult.success(info);
    }

    @ApiOperation("知识详情")
    @GetMapping("knowledgeInfo/getByIdWithAttachment/{id}")
    public AjaxResult getKnowledgeInfo(@PathVariable("id") String id) {
        KnowledgeInfo info = knowledgeInfoService.getByIdWithAttachment(id);
        return AjaxResult.success(info);
    }

    @ApiOperation("派单处理")
    @PostMapping(value = "/dispatchWorkOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderNumber", value = "工单编号", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "depts", value = "部门id、list", allowableValues = "", required = true, dataTypeClass = List.class),
            @ApiImplicitParam(name = "timeOut", value = "超时时间", allowableValues = "", required = true, dataTypeClass = String.class)
    })
    public AjaxResult dispatchWorkOrder(@RequestBody Map<String, Object> maps) {
        return woWorkOrderService.dispatchWorkOrder(maps);
    }

    @ApiOperation("工单业务流转")
    @PostMapping(value = "/sendLastWorkOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderNumber", value = "工单编号", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "taskId", value = "当前任务id", allowableValues = "", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "workOrderStatus", value = "工单状态编号", allowableValues = "", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "dept", value = "责任部门", allowableValues = "", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "buttonData", value = "按钮绑定的数据", allowableValues = "", required = true, dataTypeClass = String.class)
    })
    public AjaxResult sendLastWorkOrder(@RequestBody Map<String, Object> maps) {
        return woWorkOrderService.sendLastWorkOrder(maps);
    }

    @ApiOperation("新增延期申请")
    @PostMapping("woExtension")
    public AjaxResult add(@RequestBody WoExtension woExtension) throws Exception {
        return woExtensionService.insert(woExtension);
    }

    @ApiOperation("新增督办申请")
    @PostMapping("woSupervise")
    public AjaxResult add(@RequestBody WoSupervise woSupervise) throws Exception {
        return woSuperviseService.insert(woSupervise);
    }

    @ApiOperation("新增不计考核申请")
    @PostMapping("disregardKpi")
    public AjaxResult add(@RequestBody WoDisregardKpi disregardKpi) throws Exception {
        return disregardKpiService.insert(disregardKpi);
    }

    @ApiOperation("保存cid和uid")
    @GetMapping("saveCidUid")
    public AjaxResult saveCidUid(@NotEmpty @RequestParam String cid, @NotEmpty @RequestParam String uid) {
        cidUidRelationService.saveOrUpdate(cid, uid);
        return AjaxResult.success();
    }
}
